package com.msos.servlet.phoneDelete;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.msos.model.customer.Customer;
import com.msos.model.respBean.RespBean;
import com.msos.service.phoneDeleteService.phoneDeleteService;
import com.msos.utils.CommonsUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/phoneDelete")
public class phoDeleteServlet extends HttpServlet {
      phoneDeleteService phonedeleteService=new phoneDeleteService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag= request.getParameter("flag");
        String[] numList= request.getParameterValues("phoNumber");
        Integer r1;
        if(numList.length>0){
         r1= phonedeleteService.phoDelete(numList,flag);
        }else {
            r1=CommonsUtils.NOVALUES;
        }
        RespBean respBean=null;
        if(r1== CommonsUtils.SUCCESS){
            respBean= RespBean.ok("销户成功");
        }else if (r1==CommonsUtils.NOTHING){
            respBean=RespBean.error("numList为空");
        }else if(r1==CommonsUtils.NOTHING){
            respBean=RespBean.error("无此用户");
        }else {
            respBean=RespBean.error("发生异常");
        }
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper om=new ObjectMapper();
        PrintWriter out = response.getWriter();
        String json= om.writeValueAsString(respBean);
        out.write(json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        List<Customer> list=null;
        if("page".equals(action)){
            request.getRequestDispatcher("WEB-INF/jsp/phoneDelete/phoneDelete.jsp").forward(request,response);
        }else if("data".equals(action)){
            list=phonedeleteService.getAllCus();
            response.setContentType("application/json;charset=utf-8");
            ObjectMapper om=new ObjectMapper();
            PrintWriter out = response.getWriter();
            String json= om.writeValueAsString(list);
            out.write(json);
        }
    }
}
